-- Viral Resistance - T-Engine 4 Module
-- Copyright (C) 2010 Mikolai Fajer
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Mikolai Fajer "yufra"
-- mfajer@gmail.com

newTalentType{ type="utility/stealth", name = "stealth", description = "Stealth", generic = true }

newTalent{
	name = "Stealth",
	type = {"utility/stealth", 1},
	mode = "sustained", no_sustain_autoreset = true,
	points = 5,
	cooldown = 10,
	on_preuse = function(self, t)
		-- Check nearby actors
		local grids = core.fov.circle_grids(self.x, self.y, math.floor(10 - self:getTalentLevel(t) * 1.1), true)
		for x, yy in pairs(grids) do for y in pairs(yy) do
			local actor = game.level.map(x, y, game.level.map.ACTOR)
			if actor and actor ~= self and actor:reactionToward(self) < 0 and not rng.percent(self.hide_chance or 0) then
				game.logPlayer(self, "You cannot Stealth with nearby foes watching!")
				return nil
			end
		end end
		return true
	end,
	activate = function(self, t)
		-- Add armor penalties
		local penalties = 0
		if self:getInven("BODY") then
			local armor = self:getInven("BODY")[1]
			if armor and armor.subtype == "heavy" then
				penalties = 5
				game.logPlayer(self, "Your armour makes it difficult to be sneaky!")
			elseif armor and armor.subtype == "massive" then
				penalties = 10
				game.logPlayer(self, "Your armour makes it difficult to be sneaky!")
			end
		end

		local res = {
			stealth = self:addTemporaryValue("stealth", 15 - penalties + self:getCun(50 - penalties / 2, true) * self:getTalentLevel(t) + self:getDex(50 - penalties, true) * self:getTalentLevel(t)),
			lite = self:addTemporaryValue("lite", -1000),
			infra = self:addTemporaryValue("infravision", 1),
		}
		game.level.map:updateMap(self.x, self.y)
		return res
	end,
	deactivate = function(self, t, p)
		self:removeTemporaryValue("stealth", p.stealth)
		self:removeTemporaryValue("infravision", p.infra)
		self:removeTemporaryValue("lite", p.lite)
		game.level.map:updateMap(self.x, self.y)
		return true
	end,
	info = function(self, t)
		return ([[Enters stealth mode, making you harder to detect.
		Stealth cannot work with heavy or massive armours.
		While in stealth mode, light radius is reduced to 0.
		There needs to be no foes in sight in a radius of %d around you to enter stealth.]]):format(math.floor(10 - self:getTalentLevel(t) * 1.1))
	end,
}